package com.wensi.intelligentvoice

import android.Manifest
import android.content.Intent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.cardview.widget.CardView
import com.wensi.adapter.BasePagerAdapter
import com.wensi.helper.ARouterHelper
import com.wensi.intelligentvoice.data.MainListData
import com.wensi.intelligentvoice.voice.VoiceService
import com.wensi.lib_base.BaseActivity
import com.wensi.lib_network.HttpManager
import com.wensi.utils.L
import com.yanzhenjie.permission.Action
import com.zhy.magicviewpager.transformer.ScaleInTransformer
import kotlinx.android.synthetic.main.activity_main.*
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class MainActivity : BaseActivity() {
    private var mlist: ArrayList<MainListData> = ArrayList<MainListData>()
    private var mlistView: ArrayList<View> = ArrayList<View>()
    override fun getTitleText(): String {
        return getString(R.string.app_name)
    }

    override fun initView() {
        requestPermission()
//        testWeather()
        initViewpagerData()
        initViewpagerView()
    }

    private fun initViewpagerView() {
        mViewPager.pageMargin = 20
        mViewPager.offscreenPageLimit = mlist.size
        mViewPager.adapter = BasePagerAdapter(mlistView)
        mViewPager.setPageTransformer(true, ScaleInTransformer())

    }
    private fun initViewpagerData() {
        val widowHeight = windowManager.defaultDisplay.height
        val titleArr = resources.getStringArray(R.array.MainTitleArray)
        val colorArr = resources.getIntArray(R.array.MainColorArray)
        val iconArr = resources.obtainTypedArray(R.array.MainIconArray)

        for((index, value) in titleArr.withIndex()) {
            mlist.add(MainListData(value, iconArr.getResourceId(index, 0), colorArr[index]))
        }

        for((index, value) in mlist.withIndex()) {
            val view = View.inflate(this, R.layout.layout_main_list, null)

            val mCvMainView = view.findViewById<CardView>(R.id.mCvMainView)
            val mIvMainIcon = view.findViewById<ImageView>(R.id.mIvMainIcon)
            val mTvMainText = view.findViewById<TextView>(R.id.mTvMainText)

            mCvMainView.layoutParams?.let {cvm ->
                cvm.height = widowHeight / 5 * 3
            }

            mCvMainView.setBackgroundColor(value.color)
            mIvMainIcon.setImageResource(value.icon)
            mTvMainText.setText(value.title)

            // 点击事件
            view.setOnClickListener { _ ->
                when (value.icon) {
                    R.drawable.img_main_weather -> ARouterHelper.startActivity(ARouterHelper.PATH_WEATHER)
                    R.drawable.img_mian_contell -> ARouterHelper.startActivity(ARouterHelper.PATH_CONSTELLATION)
                    R.drawable.img_main_joke_icon -> ARouterHelper.startActivity(ARouterHelper.PATH_JOKE)
                    R.drawable.img_main_map_icon -> ARouterHelper.startActivity(ARouterHelper.PATH_MAP)
                    R.drawable.img_main_app_manager -> ARouterHelper.startActivity(ARouterHelper.PATH_APP_MANAGER)
                    R.drawable.img_main_voice_setting -> ARouterHelper.startActivity(ARouterHelper.PATH_VOICE_SETTING)
                    R.drawable.img_main_system_setting -> ARouterHelper.startActivity(ARouterHelper.PATH_SETTING)
                    R.drawable.img_main_developer -> ARouterHelper.startActivity(ARouterHelper.PATH_DEVELOPER)
                }

            }
            mlistView.add(view)
        }
    }

    private fun testWeather() {
        HttpManager.queryWeather("北京").enqueue(object: Callback<ResponseBody> {
            override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                L.i("onFailure 天气查询失败${t.toString()}")
            }

            override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
                L.i("onResponse 天气查询成功 ${response.body().toString()}")
            }
        })
    }

    override fun isShowBack(): Boolean {
        return false
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    /**
     * android 6.0 以上需要动态申请权限
     */
    private fun requestPermission() {
        // 动态权限
        if (checkPermission(Manifest.permission.RECORD_AUDIO)) {
            // 启动服务
            linkService()
        } else {
            requestPermission(arrayOf(Manifest.permission.RECORD_AUDIO), object: Action<List<String>>{
                override fun onAction(data: List<String>?) {
                    linkService()
                }
            })
        }

        // 窗口权限
        if (!checkWindowPerission()) {
            // 启动服务
            requestWindowPermission(packageName)
        }

    }

    private fun linkService () {
        startService(Intent(this, VoiceService::class.java))
    }



}
